﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Linq;
using System.Xml.Linq;
using System.Diagnostics;

namespace TransportMe
{
    public class Nimbus2
    {

        public static bool completed = false;
        public string ERPRateSet = "http://api.projectnimbus.org/ltaodataservice.svc/ERPRateSet";
        private string accountKey = "k/6DiViFKnc62/GCi9nTvg=="; //get your accountKey from http://projectnimbus.org/
        private string uniqueUserId = "a2d1c944-fb48-4cae-82bf-a17f84741bf1";// get a unique id in http://api.projectnimbus.org/tool.aspx
        public WebClient svc;

        public Nimbus2()
        {
            svc = new WebClient();
            svc.Headers["AccountKey"] = accountKey;
            svc.Headers["UniqueUserID"] = uniqueUserId;
            svc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(svc_DownloadStringCompleted);
        }

        //ERPRate
        void svc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                refreshCompleted();
                string rawXml = e.Result;
                XNamespace atomNS = "http://www.w3.org/2005/Atom";
                XNamespace dNS = "http://schemas.microsoft.com/ado/2007/08/dataservices";
                XNamespace mNS = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata";
                XNamespace gNS = "http://www.georss.org/georss";
                var results = (from item in XElement.Parse(rawXml).Descendants(atomNS + "entry")
                               let x = item.Element(atomNS + "content").Element(mNS + "properties")
                               select new ERPRate()
                               {
                                   ERPID = int.Parse(x.Element(dNS + "VehicleID").Value),
                                   VCCType = x.Element(dNS + "VCCType").Value,
                                   DayType = x.Element(dNS + "DayType").Value,
                                   ChargeAmount = double.Parse(x.Element(dNS + "ChargeAmount").Value),
                                   StartTime = x.Element(dNS + "StartTime").Value,
                                   EndTime = x.Element(dNS + "EndTime").Value,

                               });
                foreach (ERPRate er in results)
                {
                    ERPRate.listOfERPRates.Add(er);
                }

                ERPRate.listOfERPRates.Add(new ERPRate(35193, "Passenger Cars/Light Goods Vehicles/Taxis", "Weekdays", 1, "07:00", "07:05", 1.295431, 103.879795, "ECP after Tanjong Rhu Flyover, West bound"));

                completed = true;
                Debug.WriteLine(ERPRate.listOfERPRates.Count);
            }
            else
            {
                throw new Exception("NimbusConnectionException");
            }
        }

        public void refreshCompleted()
        {
            completed = false;
        }
    }
}
